﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using Milo.Eg.Domain;
using Milo.Eg.Domain.Controls;

namespace Milo.Eg.Web.Controllers
{
	public class CustomerController : Controller
	{
		private DaoFactory factory = new DaoFactory();

		public ActionResult Index()
		{
			IList<Customer> customers = factory.GetCustomers();

			CustomerListControl clc = new CustomerListControl(customers);

			//attach action method to edit trigger for each customer
			foreach (CustomerControl cc in clc)
				cc.EditTrigger.AttachTriggerAction<CustomerController>(c => c.Edit(cc.Code.Value));

			//attach trigger to go back to home page
			clc.ReturnTrigger.AttachTriggerAction<HomeController>(c => c.Index());

			return View(clc);
		}

		public ActionResult Edit(string id)
		{
			Customer customer = factory.GetCustomerByCode(id);
			CustomerControl control = new CustomerControl(customer);

			control.SaveTrigger.AttachTriggerAction<CustomerController>(c => c.Save());

			return View(control);
		}

		public ActionResult PostcodeLook(string whichAddress)
		{
			throw new NotImplementedException();
		}

		public ActionResult Save()
		{
			return RedirectToAction("Index");
		}
	}
}
